﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gmis.Public;
using System.IO;

namespace Gmis.Web.Manage.Party
{
    /// <summary>
    /// PPreMember 的摘要说明
    /// </summary>
    public class PPreMemberHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string sId = context.Request["id"];
            string action = context.Request["action"];          
            //
            if (string.IsNullOrEmpty(action))
            {
                context.Response.Write(CommonFunction.failure("无指定操作"));
            }
            else
            {
                switch (action.ToLower())
                {
                    case "add":
                        context.Response.Write(add());
                        break;
                    case "delete":
                        context.Response.Write(delete(sId));
                        break;
                    case "deleteall":
                        context.Response.Write(deleteAll(context.Request["ids"]));
                        break;
                    case "edit":
                        context.Response.Write(update());
                        break;
                }
            }
        }
        private Model.PPreMember GenerateModel()
        {
            //获取form数据
            Model.PPreMember model = new Model.PPreMember();
            model.sId = CommonFunction.getRequestValue("txt_sId");
            if (model.sId == "") model.sId = CommonFunction.GetGuid();//主键
            model.sName = CommonFunction.getRequestValue("txt_sName");//姓名    
            model.sNumber = CommonFunction.getRequestValue("txt_sNumber");//发展编号
            model.sOrigin = CommonFunction.getRequestValue("txt_sOrigin");//家庭出身
            model.sSex = CommonFunction.getRequestValue("cbx_sSex");//性别         
            model.sNation = CommonFunction.getRequestValue("cbx_sNation");//民族
            model.sEducation = CommonFunction.getRequestValue("cbx_sEducation");//学历
            model.sIdentity = CommonFunction.getRequestValue("txt_sIdentity");//身份证号
            model.dBorn = Convert.ToDateTime(CommonFunction.GetBrithdayFromIdCard(model.sIdentity));//出生日期
            model.sComposition = CommonFunction.getRequestValue("txt_sComposition");//本人成分
            model.sWork = CommonFunction.getRequestValue("txt_sWork");//职业
            model.dJoinWork = Convert.ToDateTime(CommonFunction.getRequestValue("txt_dJoinWork"));//参加工作时间
            model.sPosition = CommonFunction.getRequestValue("txt_sPosition");//现任职务
            model.dApply = Convert.ToDateTime(CommonFunction.getRequestValue("txt_dApply"));//申请时间
            model.dConfirm = Convert.ToDateTime(CommonFunction.getRequestValue("txt_dConfirm"));//确定入党积极分子时间
            model.sNative = CommonFunction.getRequestValue("txt_sNative");//籍贯
            model.sWorkExperience = CommonFunction.getRequestValue("txt_sWorkExperience");//主要工作经历            
            model.sSurvey1 = CommonFunction.getRequestValue("txt_sSurvey1");//培养考察人一
            model.sSurvey2 = CommonFunction.getRequestValue("txt_sSurvey2");//培养考察人二
            model.sMobile = CommonFunction.getRequestValue("txt_sMobile");//手机号码
            model.sPicture = Path.GetFileName(CommonFunction.getRequestValue("txt_sPicture"));//图片路径
            model.iPicture = null;
            return model;
        }
        //增加一条
        private string add()
        {
            BLL.PPreMember bll = new BLL.PPreMember();
            if (bll.Add(GenerateModel()))
            {                
                return CommonFunction.successAndCloseCurrent("", "", "PPreMember");
            }
            else
            {
                return CommonFunction.failure();
            }
        }
        //删除一条
        private string delete(string sId)
        {
            BLL.PPreMember bll = new BLL.PPreMember();
            if (bll.Delete(sId))
            {
                return CommonFunction.success();
            }
            else
            {
                return CommonFunction.failure();
            }
        }
        //删除全部
        private string deleteAll(string ids)
        {
            string[] strs = ids.Split(',');
            try
            {
                BLL.PPreMember bll = new BLL.PPreMember();
                foreach (string str in strs)
                {
                    bll.Delete(str);
                }
                return CommonFunction.success();
            }
            catch { return CommonFunction.failure(); }


        }
        //更新数据
        private string update()
        {
            BLL.PPreMember bll = new BLL.PPreMember();
            if (bll.Update(GenerateModel()))
            {
                return CommonFunction.successAndCloseCurrent("", "", "PPreMember");
            }
            else
            {
                return CommonFunction.failure();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}